package model.audio;

public class Filter {

	public Filter(){
		
	}
	
	public Complex[] frequency_filter(float sampleRate,Complex fft_out[],float low,float high){     //将频率在 low~high之间(包括low high)的信号过滤掉,最高能过滤掉max_frequency的频率
		float max_frequency=sampleRate/2;
		if(low>high){throw new RuntimeException("please confirm high is bigger than low");}
		if(low>max_frequency){throw new RuntimeException("max_frequency can filter = "+max_frequency);}
		if(high>max_frequency){
			high=max_frequency;
		}
		int N=fft_out.length;
		float frequencePerPoint=sampleRate/N;
		for(int i=0;i<N;i++){
			float frequency=frequencePerPoint*i;
			if(frequency>=low&&frequency<=high){
				fft_out[i]=new Complex(0, 0);       //FFT结果共轭，得到复数数组左右对称，需要对称的清零
				fft_out[N-1-i]=new Complex(0,0);          //对称清零
			}
		}
		return fft_out;
	}
	
/*	public Complex[] sound_filter(Complex fft_ref[],Complex fft_out[]){      //不对
		int N=fft_ref.length;
		for(int i=0;i<N;i++){
			fft_out[i]=new Complex(fft_out[i].re()-fft_ref[i].re(),fft_out[i].im()-fft_ref[i].im());
		}
		return fft_out;
	}*/
	
}
